/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package com.chinalwb.are.view;

import com.chinalwb.are.ResourceTable;
import com.chinalwb.are.models.ColorPickerModel;
import com.chinalwb.are.provider.ColorPickerProvider;
import com.chinalwb.are.utils.FindUtils;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextTool;
import ohos.agp.window.dialog.PopupDialog;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * 颜色选择
 *
 * @author zh
 * @since 2021/06/22
 */
public class ColorPickerDialog extends PopupDialog {
    private Component dialogView;
    private final Context context;
    private ColorPickerProvider colorPickerProvider;
    private final List<ColorPickerModel> colorPickerModels = new ArrayList<>();
    private final String[] colors = new String[]{"#000000", "#607D8B",
            "#9E9E9E", "#795548", "#FF5722", "#FF9800", "#FFC107",
            "#FFEB3B", "#CDDC39", "#8BC34A", "#4CAF50", "#009688", "#00BCD4", "#03A9F4",
            "#2196F3", "#3F51B5", "#673AB7", "#9C27B0", "#E91E63", "#f44336"};
    private String selectColor;

    public ColorPickerDialog(Context context, Component contentComponent, String color) {
        super(context, contentComponent);
        this.context = context;
        this.selectColor = color;
        initView();
    }

    private void initView() {
        dialogView = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_dialog_color_picker, null, false);
        setAlignment(LayoutAlignment.TOP);
        setCustomComponent(dialogView);
        setBackColor(Color.TRANSPARENT);
        setTransparent(true);
        ListContainer listContainer = FindUtils.findListById(dialogView, ResourceTable.Id_listview);
        for (String color : colors) {
            ColorPickerModel model = new ColorPickerModel();
            model.setColor(color);
            model.setSelect(selectColor.equals(color));
            colorPickerModels.add(model);
        }
        colorPickerProvider = new ColorPickerProvider(colorPickerModels, context);
        listContainer.setItemProvider(colorPickerProvider);
        setDialogListener(() -> {
            setRemoveDialog();
            return false;
        });
        listContainer.setItemClickedListener((listContainer1, component, position, l) -> {
            for (int i = 0; i < colors.length; i++) {
                if (position == i) {
                    colorPickerModels.get(i).setSelect(true);
                    selectColor = colorPickerModels.get(i).getColor();
                } else {
                    colorPickerModels.get(i).setSelect(false);
                }
            }
            colorPickerProvider.notifyDataChanged();
        });
    }

    /**
     * 销毁dialog
     */
    private void setRemoveDialog() {
        dialogView = null;
        remove();
        destroy();
        if (!TextTool.isNullOrEmpty(selectColor)) {
            selectColorListener.onSelectColorListener(selectColor);
        }
    }

    private SelectColorListener selectColorListener;

    /**
     * selectColorListener
     *
     * @param selectColorListener selectColorListener
     */
    public void setSelectColorListener(SelectColorListener selectColorListener) {
        this.selectColorListener = selectColorListener;
    }

    /**
     * SelectColorListener
     */
    public interface SelectColorListener {
        void onSelectColorListener(String color);
    }
}
